package com.example.common.provider;

import com.example.common.ResourceTable;
import com.example.common.model.bean.JavaCourse;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

import java.util.List;

public class ItemProvider extends BaseItemProvider {
    private List<JavaCourse> list;
    private Context context;
    public ItemProvider(List<JavaCourse> list, Context slice) {
        this.list = list;
        this.context = slice;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position >= 0 && position < list.size()){
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_java_hoder_unit, null, false);
        } else {
            cpt = convertComponent;
        }
        JavaCourse sampleItem = list.get(position);
        return cpt;
    }

}
